package com.tjetc.service.impl;

import com.tjetc.dao.CategoryDao;
import com.tjetc.dao.MedicineDao;
import com.tjetc.dao.PhotoDao;
import com.tjetc.dao.impl.CategoryDaoImpl;
import com.tjetc.dao.impl.MedicineDaoImpl;
import com.tjetc.dao.impl.PhotoDaoImpl;
import com.tjetc.domain.Category;
import com.tjetc.domain.Medicine;
import com.tjetc.domain.Photo;
import com.tjetc.service.MedicineService;
import com.tjetc.utils.Db;
import com.tjetc.utils.PageInfo;

import java.sql.SQLException;
import java.util.List;

public class MedicineServiceImpl implements MedicineService {
    private MedicineDao medicineDao=new MedicineDaoImpl();
    private CategoryDao categoryDao=new CategoryDaoImpl();
    private PhotoDao photoDao=new PhotoDaoImpl();
    @Override
    public int getCount(String name) {
        try {
            return medicineDao.getCount(name);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Medicine> list(String name, PageInfo<Medicine> pageInfo) {
        try {
            List<Medicine> list=medicineDao.list(name,pageInfo);
            for (Medicine medicine : list) {
                Category category=categoryDao.getById(medicine.getCid());
                medicine.setCategory(category);
                medicine.setPhotos(photoDao.findByMid(medicine.getId()));
            }

            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean add(Medicine medicine, List<String> photopaths) {
        try {
            //开启事务
            Db.startTransaction();
            medicineDao.add(medicine);
            //取得id
            int id=medicineDao.findMaxId();
            //循环photopaths->photo
            for (String photopath : photopaths) {
                Photo photo =new Photo();
                photo.setMid(id);
                photo.setPhotopath(photopath);
                photoDao.add(photo);
            }
            //提交事务
            Db.commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            //回滚事务
            Db.rollback();
        } finally {
            //关闭连接
            Db.close();
        }
        return false;
    }

    @Override
    public boolean update(Medicine medicine, List<String> photopaths, String paths) {
        /*paths保留的旧的，photopaths新上传的*/
        try {
            //开启事务
            Db.startTransaction();
            //先删除数据库中原有的照片
            photoDao.delByMid(medicine.getId());
            //旧的
            String[] split=paths.split(",");
            for (String s : split) {
                Photo photo = new Photo();
                photo.setMid(medicine.getId());
                photo.setPhotopath("upload/"+s);
                photoDao.add(photo);
            }
            //新的
            for (String photopath : photopaths) {
                Photo photo=new Photo();
                photo.setMid(medicine.getId());
                photo.setPhotopath(photopath);
                photoDao.add(photo);
            }
            //medicine
            medicineDao.update(medicine);
            //提交事务
            Db.commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            //回滚事务
            Db.rollback();
        } finally {
            //关闭
            Db.close();
        }
        return false;
    }

    @Override
    public boolean del(int id) {
        try {
            Db.startTransaction();
            photoDao.delByMid(id);
            medicineDao.delById(id);
            Db.commit();
            return  true;
        } catch (Exception e) {
            e.printStackTrace();
            Db.rollback();
        } finally {
            Db.close();
        }
        return false;
    }

    @Override
    public List<Medicine> listAll() {
        try {
            return medicineDao.listAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Medicine getById(int id) {
        try {
            return medicineDao.getById(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Medicine findByName(String name) {
        try {
            return medicineDao.findByName(name);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
